package ui.fragment

import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.CheckBox
import androidx.fragment.app.Fragment
import com.chillibits.simplesettings.core.SimpleSettings
import com.chillibits.simplesettings.core.SimpleSettingsConfig
import com.example.mynewsapp.R
import com.example.mynewsapp.databinding.FragmentSettingBinding
import com.fede987.statusbaralert.StatusBarAlert
import com.fede987.statusbaralert.utils.statusBarAlert

class SettingFragment : Fragment() {
    private lateinit var binding: FragmentSettingBinding


    val handler: Handler = Handler()
    var alert1: StatusBarAlert? = null
    var alert2: StatusBarAlert? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        binding = FragmentSettingBinding.inflate(inflater, container, false)

        // ref link:
        // https://github.com/marcauberer/simple-settings/blob/main/app/src/main/java/com/chillibits/simplesettingssample/MainActivity.kt
//        SimpleSettings(requireActivity()).show {
//
//            Section {
//                Page {
//                    title = "配置1"
//                    summary = "总结"
//                    displayHomeAsUpEnabled = false
//
//
//                }
//            }
//        }

        binding.openXmlBtn.setOnClickListener {
            openSettingsXmlConfig()
        }
        setupButtons(binding.root)

        return binding.root
    }


    override fun onDestroy() {
        alert1 = null
        alert2 = null
        handler.removeCallbacksAndMessages(null)
        super.onDestroy()
    }


    private fun openSettingsXmlConfig() {
        val config = SimpleSettingsConfig.Builder().displayHomeAsUpEnabled(true)
            .setOptionsMenu(R.menu.setting_page_menu,
                // ref link:
                // https://github.com/marcauberer/simple-settings/blob/main/app/src/main/java/com/chillibits/simplesettingssample/MainActivity.kt#L243
                object : SimpleSettingsConfig.OptionsItemSelectedCallback {
                    override fun onSettingsOptionsItemSelected(itemId: Int) {
                        when (itemId) {
                            R.id.actionGitHub -> openGitHubPage()
//                            R.id.actionRate -> openGooglePlayAppSite()
                        }
                    }
                }).setPreferenceCallback(object : SimpleSettingsConfig.PreferenceCallback {

            }).showResetOption(true)
            .setPendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right)
            .build()

        // Settings data from xml resource to keep a better overview
        SimpleSettings(requireActivity(), config).show(R.xml.preferences)
    }

    // 处理打开github
    private fun openGitHubPage() {
        startActivity(Intent(Intent.ACTION_VIEW).apply {
            // 打开我自己的gitee
            data = Uri.parse("https://gitee.com/eeppee_admin")
        })
    }


    private fun setupButtons(view: View) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            view.findViewById<CheckBox>(R.id.dark_status_checkbox)
                .setOnCheckedChangeListener { button, checked ->
//                if (checked) window.decorView.systemUiVisibility =
//                    window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
//                else window.decorView.systemUiVisibility =
//                    window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
                }
        } else view.findViewById<CheckBox>(R.id.dark_status_checkbox).visibility = View.GONE

        view.findViewById<Button>(R.id.button1).setOnClickListener {

            alert1 = StatusBarAlert.Builder(requireActivity()).autoHide(true).showProgress(true)
                .duration(10000).text("autohide!").alertColor(R.color.dark_red)
                .progressBarColor(R.color.splash_page_color).textColor(R.color.black).build()
                .apply { show() }

            handler.postDelayed({
                alert1?.setText("Phase 1!")
                alert1?.showProgress()
            }, 2000)


            handler.postDelayed({
                alert1?.setText("Phase 2!")
                alert1?.showProgress()
            }, 4000)

            handler.postDelayed({
                alert1?.setText("Completed!")
                alert1?.hideProgress()
            }, 7500)

        }

        view.findViewById<Button>(R.id.button2).setOnClickListener {
            alert2 = statusBarAlert {
                autoHide(false)
                showProgress(false)
                text("RED ALERT!")
                alertColor(R.color.dark_red)
                textColor(R.color.black)
            }.show()

            handler.postDelayed({
                if (alert2?.parent != null)
                    alert2?.setText("INFO UPDATED!!")
            }, 2000)
        }

        view.findViewById<Button>(R.id.button3).setOnClickListener {
            statusBarAlert {
                autoHide()
                duration(400)
                showProgress(true)
                text("Blink")
                alertColor(R.color.dark_red)
            }.show()
        }
    }
}